package com.adityayadav.parallelism.core;

import java.util.UUID;

public class Thread extends java.lang.Thread {
	
	protected ThreadPool threadPool;
	protected String threadUUID;
	protected boolean active = true;
	protected boolean idle = true;
	
	public Thread(ThreadPool threadPool){
		this.threadPool = threadPool;
		this.threadUUID = UUID.randomUUID().toString();
	}
	
	@Override
	public void run(){
		while (active){
			idle = true;
			ITask task = null;
			try {
				task = threadPool.takeTask();
				if (task != null){
					idle = false;
					task.initialize();
					task.run();
					task.commit();
				}
			} catch (Exception e){
				try {
					task.rollback();
				} catch (Exception f){}
			}
		}
		threadPool.removeThread(this);
	}

	public boolean isActive() {
		return active;
	}

	public void setActive(boolean active) {
		this.active = active;
	}

	public boolean isIdle() {
		return idle;
	}
	
	
}
